package com.kdtech.analyse.Bbs;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.StringUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;

public class KdnetBbsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://club.kdnet.net/dispbbs.asp\\?f=w&ctid=[0-9]*&boardid=[0-9]*&id=[0-9]*"
				,"http://club.kdnet.net/dispbbs.asp\\?id=[0-9]*\\&boardid=[0-9]*"
				,"http://club.kdnet.net/dispbbs.asp\\?boardid=[0-9]*&id=[0-9]*"
				,"http://club.kdnet.net/dispbbs.asp\\?page=1&boardid=[0-9]*&id=[0-9]*"
				,"http://club.kdnet.net/dispbbs.asp\\?boardid=[0-9]*&page=1&id=[0-9]*"
				,"http://blog.kdnet.net/boke.asp\\?id[0-9]*.showtopic.[0-9]*.html"
		};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta bbs = new NewsMeta();
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();

		bbs.setUrl(url);
		String title = null;
		String content = null;
		Long date = null;
		Integer clickNum = 0;
		Integer commentNum = 0;
		String author=null;
		Document doc = Jsoup.parse(htmltxt);
		if(url.startsWith("http://blog.kdnet.net/")){
			 doc.select(".topic .topic_r").remove();
			title = doc.select(".topic").text();
			date = JSoupUtils.matchDate(doc, "发表于：");
			content = HtmlCleaner.getContentHtml(url, doc.select("#ShowBody"));
			author = doc.select(".postend a").text();
		}else{
			title = doc.select("div.postspecific div.posts-title").text();
			date = DateUtils.matchDate(doc.select("div.postspecific div.posts-posted").text());
			content = HtmlCleaner.getContentHtml(url, doc.select("div.postspecific div.posts-cont"));
			String text = doc.select("html body#ClubCont div.wrapper div.lf div.posted-box-add div.postspecific div.posts-stat-box div.posts-stat-c div span.f10px:eq(0)").text();
			try {
				int pos = text.indexOf(" ");
				if(pos!=-1){
					text =text.substring(0,pos);
				}
				clickNum = Integer.parseInt(text);
			} catch (NumberFormatException e) {
			}
			String text1 = doc.select("html body#ClubCont div.wrapper div.lf div.posted-box-add div.postspecific div.posts-stat-box div.posts-stat-c div.forward-wblog span.f10px").text();
			try {
				commentNum = Integer.parseInt(text1);
			} catch (NumberFormatException e) {
			}
			Elements select2 = doc.select("html body#ClubCont div.wrapper div.lf div.posted-box-add div.postspecific div.posts-posted span.c-main a");
			if(select2!=null && select2.size()!=0){
				author=select2.get(0).text();
			}
		}
		bbs.setAuthor(author);
		bbs.setTitle(title);
		bbs.setContent(content);
		bbs.setDate(date);
		/**
		 * 解析用于更新的地址
		 */
		bbs.setUpdateUrl(url);

		bbs.setClickNum(clickNum);
		bbs.setCommentNum(commentNum);



		return bbs;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	
}
